package com.tengju.data.infrastructure.repository.clickhouse.crm;

import com.google.common.collect.Lists;
import com.tengju.data.domain.model.analysis.Row;
import com.tengju.data.domain.model.brand.BrandSaleRankQueryForm;
import com.tengju.data.domain.model.clickhouse.DwsCrmBrandAnalysisDRepository;
import com.tengju.data.domain.model.user.DominationLevelEnum;
import com.tengju.data.domain.model.user.IsDirect;
import com.tengju.data.domain.model.user.StarLevelEnum;
import com.tengju.data.domain.shared.IdObject;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.clickhouse.crm.DwsCrmBrandAnalysisDMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@Repository
public class DwsCrmBrandAnalysisDRepositoryImpl implements DwsCrmBrandAnalysisDRepository {

    @Resource
    DwsCrmBrandAnalysisDMapper dwsCrmBrandAnalysisDMapper;


    @Override
    public Integer count(BrandSaleRankQueryForm form){

        List<Integer> starLevels = form.getCrmRankCrowdDatePerspective()
                .getCrmRankCrowdPerspective().getStarLevels()
                .stream().map(StarLevelEnum::getCode).collect(Collectors.toList());

        List<Long> idCodes = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(form.getIdCodes())) {
            idCodes = form.getIdCodes()
                    .stream().map(IdObject::getValue).collect(Collectors.toList());
        }

        return dwsCrmBrandAnalysisDMapper.count(Optional.ofNullable(form.getDominationIdCode()).map(IdObject::getValue).orElse(0L),
                Optional.ofNullable(form.getDominationLevel()).map(DominationLevelEnum::getVal).orElse(null),
                starLevels,idCodes,form.getMonth(),form.getQuarter(),form.getBrandId().getValue(),form.getSaleMoneyFrom(),form.getSaleMoneyTo(),
                Optional.ofNullable(form.getIsDirect()).map(IsDirect::getVal).orElse(null),form.getCrmRankCrowdDatePerspective().getSqlGmvField(),
                form.getCrmRankCrowdDatePerspective().getSqlRankField(),form.getCrmRankCrowdDatePerspective().getMonthQuarterEnum().getCode());
    }

    @Override
    public BigDecimal totalSaleMoney(BrandSaleRankQueryForm form){

        List<Integer> starLevels = form.getCrmRankCrowdDatePerspective()
                .getCrmRankCrowdPerspective().getStarLevels()
                .stream().map(StarLevelEnum::getCode).collect(Collectors.toList());

        List<Long> idCodes = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(form.getIdCodes())) {
            idCodes = form.getIdCodes()
                    .stream().map(IdObject::getValue).collect(Collectors.toList());
        }

        return dwsCrmBrandAnalysisDMapper.totalSaleMoney(Optional.ofNullable(form.getDominationIdCode()).map(IdObject::getValue).orElse(0L),
                Optional.ofNullable(form.getDominationLevel()).map(DominationLevelEnum::getVal).orElse(null),
                starLevels,idCodes,form.getMonth(),form.getBrandId().getValue(),form.getSaleMoneyFrom(),form.getSaleMoneyTo(),
                Optional.ofNullable(form.getIsDirect()).map(IsDirect::getVal).orElse(null),form.getCrmRankCrowdDatePerspective().getSqlGmvField(),
                form.getQuarter(), form.getCompareQuarter(),form.getCrmRankCrowdDatePerspective().getMonthQuarterEnum().getCode(),form.getCrmRankCrowdDatePerspective().getSqlRankField());
    }


    @Override
    public List<Row> brandAnalysis(BrandSaleRankQueryForm form) {

        List<Integer> starLevels = form.getCrmRankCrowdDatePerspective()
                .getCrmRankCrowdPerspective().getStarLevels()
                .stream().map(StarLevelEnum::getCode).collect(Collectors.toList());

        List<Long> idCodes = Lists.newArrayList();
        if (!CollectionUtils.isEmpty(form.getIdCodes())) {
            idCodes = form.getIdCodes()
                    .stream().map(IdObject::getValue).collect(Collectors.toList());
        }

        List<HashMap<String, Object>> sqlResult = dwsCrmBrandAnalysisDMapper
                .brandAnalysis(Optional.ofNullable(form.getDominationIdCode()).map(IdObject::getValue).orElse(0L),
                        Optional.ofNullable(form.getDominationLevel()).map(DominationLevelEnum::getVal).orElse(null),
                        starLevels, idCodes, form.getMonth(), form.getCompareMonth(), form.getQuarter(), form.getCompareQuarter(), form.getBrandId().getValue(), form.getSaleMoneyFrom(),
                        form.getSaleMoneyTo(), Optional.ofNullable(form.getIsDirect()).map(IsDirect::getVal).orElse(null),
                        form.getCrmRankCrowdDatePerspective().getSqlRankField(), form.getCrmRankCrowdDatePerspective().getSqlDealerMemberField(),
                        form.getCrmRankCrowdDatePerspective().getSqlGmvField(), form.getCrmRankCrowdDatePerspective().getCrmRankCrowdPerspective().getVal(),
                        form.getBrandAnalysisSortType().getSql(), form.getOffset(), form.getPageSize(),form.getCrmRankCrowdDatePerspective().getMonthQuarterEnum().getCode());

        return sqlResult.stream().map(Row::new)
                .collect(Collectors.toList());

    }
}
